package com.example.demo.thread.生产消费模型;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

public class testProC {

    public static void main(String[] args) {
        try {
            BlockingDeque<Data> queue = new LinkedBlockingDeque<>(10);

            Producer producer1 = new Producer(queue);
            Producer producer2 = new Producer(queue);
            Producer producer3 = new Producer(queue);

            Consumer consumer1 = new Consumer(queue);
            Consumer consumer2 = new Consumer(queue);
            Consumer consumer3 = new Consumer(queue);

            ExecutorService service = Executors.newCachedThreadPool();
            service.execute(producer1);
            service.execute(producer2);
            service.execute(producer3);
            service.execute(consumer1);
            service.execute(consumer2);
            service.execute(consumer3);

            Thread.sleep(4000);
            producer1.stop();
            producer2.stop();
            producer3.stop();

            Thread.sleep(1000);
            service.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
